package com.course.controller;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import com.course.db.CourseBasic;
import com.et.mvc.JspView;

public class AdminController   extends ApplicationController {

	public JspView checkCourse() throws Exception{

		System.out.println("checkcourse");
		JspView view = new JspView("admin/courseApplyApprove.jsp");
		CourseBasic course = new CourseBasic();
		
		ResultSet courseRs = course.wait_check();
	       
	       List< Hashtable<String, String> > waitcheckList = new ArrayList< Hashtable<String, String> >();
			try{
				String value="";
				ResultSetMetaData courseRsMd = courseRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (courseRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(courseRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					waitcheckList.add(hash);
				}
				
				session.setAttribute("WaitcheckList", waitcheckList);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			courseRs = course.show_checked();
		       
		       List< Hashtable<String, String> > checkedList = new ArrayList< Hashtable<String, String> >();
				try{
					String value="";
					ResultSetMetaData courseRsMd = courseRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (courseRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (courseRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(courseRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						checkedList.add(hash);
					}
					
					session.setAttribute("CheckedList", checkedList);
					
				}catch(Exception e){
					e.printStackTrace();
				}
				response.sendRedirect("../admin/courseApplyApprove.jspx");
		return null;
	}
	
	public JspView adminCheck() throws Exception{
		
		System.out.println("adminCheck");
		System.out.println("id:"+request.getParameter("courseId").trim());
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		session.setAttribute("CheckCourse", course);
		
		return null;
	}
	
public JspView detail() throws Exception{
		
		System.out.println("detail");
		System.out.println("id:"+request.getParameter("courseId").trim());
		int courseId = Integer.parseInt( request.getParameter("courseId").trim());
		CourseBasic course =new CourseBasic();
		course.setId(courseId);
		course.init();
		request.setAttribute("CheckCourse", course);
		response.sendRedirect("../teacher/courseApplyApproveDetail.jspx");
		
		return null;
	}
}
